package com.yupi.yuso.designpattern.adapter;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.yuso.designpattern.register.DataSourceRegister;
import com.yupi.yuso.model.dto.search.SearchRequest;
import com.yupi.yuso.model.dto.user.UserQueryRequest;
import com.yupi.yuso.model.enums.SearchTypeEnum;
import com.yupi.yuso.model.vo.UserVO;
import com.yupi.yuso.service.UserService;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * @author vvv
 * @date 2023-12-21 21 59
 * discription
 */
@Component
public class UserDataSourceAdapter implements DataSourceTarget<UserVO> {

    @Resource
    private UserService userService;

    @PostConstruct
    public void registerInstance() {
        DataSourceRegister.SEARCH_MAP.put(SearchTypeEnum.USER.getValue(), this);
    }

    @Override
    public Page<UserVO> doSearch(SearchRequest searchRequest) {
        UserQueryRequest userQueryRequest
                = BeanUtil.copyProperties(searchRequest, UserQueryRequest.class);
        userQueryRequest.setUserName(searchRequest.getSearchText());
        Page<UserVO> userVOPage = userService.listUserVOByPage(userQueryRequest);
        return userVOPage;
    }
}
